#include "../main.h"

#define THREAD_NUMBER 10

void *start_routine(void *);

int count = 0;
pthread_mutex_t mutex;

int main()
{
    pthread_t thread_id[THREAD_NUMBER]; 
    
    pthread_mutex_init(&mutex,NULL);

    for(int i = 0; i < THREAD_NUMBER; i++){
        pthread_create(thread_id+i,NULL,start_routine,NULL);
    }

    for(int i = 0; i < THREAD_NUMBER; i++){
        pthread_join(thread_id[i],NULL);
    }

    pthread_mutex_destroy(&mutex);

    return 0;
}

void* start_routine(void *arg)
{
    for(int i = 0;i < 1000;i++ )
    {
        pthread_mutex_lock(&mutex);
        printf("%ld %d\n",pthread_self(),count++);
        pthread_mutex_unlock(&mutex);
        usleep(1000);
    }
    pthread_exit(NULL);
}
